const urlParams = new URLSearchParams(window.location.search);
const pName= urlParams.get('pName');
let id=urlParams.get('id')
const loginName=window.sessionStorage.getItem("loginName")
const vm=new Vue({
    el:'#esproduct',
    data:{
        pageInfo:'',
        name:'',
        imgStr:'http://192.168.2.106:8848/images/',
        infoFlag:false,
        infoText:'',
        collectionBox:[]
    },
    methods:{
        //获取收藏
        getCollectionBox()
        {
            axios.get("/api/getCollectionBox",
                {
                    params:{
                        loginName
                    }
                }).then(rs=>{
                this.collectionBox=rs.data.collectionBox
            })
        },
        //获取es中的商品
        getEsPageInfo(pageNum,pageSize)
        {
            axios.get('/api/getEsProductPageInfo',
                {
                    params:{
                        pageNum,
                        pageSize,
                        name:this.name,
                        id
                    }
                }).then(rs=>{
                    this.pageInfo=rs.data.pageInfo
                    this.getCollectionBox()
            })
        },
        opera(sign)
        {
            let pageNum=1
            switch(sign)
            {
                case "pre":
                    pageNum=this.pageInfo.pageNum-1>=1?this.pageInfo.pageNum-1:1
                    break;
                case "next":
                    pageNum=this.pageInfo.pageNum+1<this.pageInfo.pages?this.pageInfo.pageNum+1:this.pageInfo.pages
                    break;
                case "last":
                    pageNum=this.pageInfo.pages
                    break;
            }
            this.getEsPageInfo(pageNum,16)
        },
        addIntroCar(product)
        {
            if(confirm("是否加入购物车？"))
            {
                const formData=new FormData()
                    let pro={id:product.id,name:product.name,price:product.price,fileName:product.fileName,num:1,
                        numPrice:product.price,stock:product.stock}
                    formData.append("pro",JSON.stringify(pro))
                    formData.append("loginName",loginName)
                    axios.post('/api/car/addIntoCar',
                            formData
                        ).then(rs=>{
                            if(rs.data.isAdd===true)
                            {
                                alert("加入购物车成功")
                                vm2.car=rs.data.car
                            }else if(rs.data.isAdd===false){
                                alert("库存不足")
                            }
                    })
            }
        },
        //添加收藏
        intoCollection(product)
        {
            const formData=new FormData
            let p={id:product.id,name:product.name,fileName:product.fileName,price:product.price}
            formData.append("loginName",loginName)
            formData.append("collectionBox",JSON.stringify(p))
            axios.post('/api/intoCollectionBox',
                formData
            ).then(rs=>{
                if(rs.data.isAdd==="yes")
                {
                    this.getCollectionBox()
                    this.infoText="收藏成功"
                    this.infoFlag=true
                    setTimeout(()=>
                    {
                        this.infoFlag=false
                    },700)
                }else if(rs.data.isAdd==="repeat")
                {
                    this.infoText="商品已经收藏"
                    this.infoFlag=true
                    setTimeout(()=>
                    {
                        this.infoFlag=false
                    },700)
                }else{
                    this.infoText="商品收藏出问题了"
                    this.infoFlag=true
                    setTimeout(()=>
                    {
                        this.infoFlag=false
                    },700)
                }

            })
        },

    },
    created()
    {
        this.name=pName
        console.log(id)
        this.getEsPageInfo(1,16)
    }
})

const vm2=new Vue({
    el:'#top',
    data:{
        name:'',
        carDetails:[],
        imgStr:'http://192.168.2.106:8848/images/',
        car:{}
    },
    methods:{
        btn_sousuo()
        {
            id=null
            vm.name=this.name
            vm.getEsPageInfo(1,16)
        },
        getCarFromRedis()
        {
            axios.get("/api/car/getCar",{
                params:{
                    loginName
                }
            }).then(rs=>{
                this.car=rs.data.car
                console.log(this.car)
            })
        },
        showCar()
        {
            this.getCarFromRedis()
        }
    },
    // created() {
    //
    // }
})